package echoTCP;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;


public class MyTcpEchoServer {

    ServerSocket serverSocket = null;

    // 进行连接
    public MyTcpEchoServer(int port) {
        try {
            // 进行连接
            serverSocket = new ServerSocket(port);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public  void start() {
        // 开始交互

        System.out.println("服务器开始运行...");
        while(true) {
        // 使用线程池
        Executor executor = Executors.newCachedThreadPool();

        executor.execute(() -> {


            Socket socket = null;

            try {
                socket = serverSocket.accept();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }

            // 进行接收请求并响应
            ProcessServerConnect(socket);


        });
        }


//        使用多线程

//        while(true) {
//            Thread t = new Thread(()->{
//
//                Socket socket = null;
//                try {
//                    socket = serverSocket.accept();
//                } catch (IOException e) {
//                    throw new RuntimeException(e);
//                }
//
//                // 进行接收请求并响应
//                ProcessServerConnect(socket);
//
//
//            });
//            t.start();
//        }




    }

    private void ProcessServerConnect(Socket socket) {
            try(InputStream inputSTream = socket.getInputStream();
                OutputStream outputStream = socket.getOutputStream()
            ) {


                while(true) {

                    System.out.printf("客户端上线 : [客户端ip = %s,客户端端口号 = %d]\n",socket.getInetAddress(),socket.getPort());
                    // 得到请求
                    Scanner scanner = new Scanner(inputSTream);
                    if(!scanner.hasNext()) {
                        break;
                    }

                    // 得到请求
                    String request = scanner.nextLine();

                    // 计算响应
                    // 执行业务
                   String response =  process(request);

                   // 返回响应
                   // 让客户端得到业务的执行结果
                   PrintWriter printWriter = new PrintWriter(outputStream);

                   // 开始返回
                   printWriter.println(response);
                   // 进行刷新
                    printWriter.flush();


//                    打印日志
                    System.out.printf("客户端下线 :  [ip = %s , port = %d], 请求 = %s , 响应 = %s\n",
                            socket.getInetAddress(),socket.getPort(),request,response);


                }



            } catch (IOException e) {
                throw new RuntimeException(e);
            } finally {
                System.out.printf("任务执行结束： [%s,%d]\n",socket.getInetAddress(),socket.getPort());
                try {
//                    关闭 socket 流
                    socket.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }

    }

    private String process(String request) {
        return  request;
    }


    public static void main(String[] args) {
        MyTcpEchoServer myTcpEchoServer = new MyTcpEchoServer(9090);
        myTcpEchoServer.start();
    }
}
